/**
 * Copyright 2009 Wisdom
 */
package com.wisdom.model.entities;

import java.util.Set;

import javax.persistence.*;

/**
 * Entity represent of user roles in system
 * 
 * @author alekseiko
 * @since 1.0
 */
@Entity
@Table(name = "WE_ROLES")
public class Role {

    private long id;
    private String role;
    private Set<User> users;

    /**
     * Default constructor.
     */
    public Role() {
    }

    /**
     * Constructor initialize all fields
     * 
     * @param id
     *            - unique role id in system\
     * 
     * @param role
     *            - user role in system
     */
    public Role(long id, String role) {
	this.id = id;
	this.role = role;
    }

    /**
     * Get unique role id
     * 
     * @return unique role id
     */
    @Id
    @Column(name = "WE_ID")
    @GeneratedValue(strategy = GenerationType.SEQUENCE)
    public long getId() {
	return id;
    }

    /**
     * Set unique role id in system
     * 
     * @param id
     *            - unique role id
     */
    public void setId(long id) {
	this.id = id;
    }

    /**
     * Get user role
     * 
     * @return user role in system
     */
    @Column(name = "WE_ROLE", unique = true, nullable = false)
    public String getRole() {
	return role;
    }

    /**
     * Set user role
     * 
     * @param role
     *            - user role in system
     */
    public void setRole(String role) {
	this.role = role;
    }

    /**
     * Get users with this role
     * 
     * @return the users
     */
    @ManyToMany(targetEntity = com.wisdom.model.entities.User.class, 
	    mappedBy = "roles", 
	    cascade = {CascadeType.PERSIST, CascadeType.MERGE })
    public Set<User> getUsers() {
	return users;
    }

    /**
     * Set users with this role
     * 
     * @param users
     *            - set users with this role
     */
    public void setUsers(Set<User> users) {
	this.users = users;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public int hashCode() {
	final int prime = 31;
	int result = 1;
	result = prime * result + (int) (id ^ (id >>> 32));
	return result;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public boolean equals(Object obj) {
    	if (this == obj)
    		return true;
    	if (obj == null)
    		return false;
    	if (getClass() != obj.getClass())
    		return false;
    	Role other = (Role) obj;
	
    	return role.equals(other.getRole());
	}

}
